module top(
           input clk,
           input rst_n,
           output reg [13: 0] q
       );

reg [13: 0] next;
reg [13: 0] temp;

integer k;
always@(posedge clk or negedge rst_n)
	begin
		if (!rst_n)
			q <= 14'd0;
		else if (q == 14'b11_0100_0000_1100)
			//计数到10231,清零
			q <= 14'd0;
		else
			q <= next;
	end

always@(q)
	begin
		temp[13] = q[13];
		for (k = 12;k >= 0;k = k - 1)
			temp[k] = q[k] ^ temp[k + 1];
		if (temp[0] == 1'b0)
			begin
				next[0] = ~q[0];
				next[13: 1] = q[13: 1];
			end
		else if (temp[1] == 1'b0)
			begin
				next[0] = q[0];
				next[1] = ~q[1];
				next[13: 2] = q[13: 2];
			end
		else if (temp[2] == 1'b0)
			begin
				next[1: 0] = q[1: 0];
				next[2] = ~q[2];
				next[13: 3] = q[13: 3];
			end
		else if (temp[3] == 1'b0)
			begin
				next[2: 0] = q[2: 0];
				next[3] = ~q[3];
				next[13: 4] = q[13: 4];
			end
		else if (temp[4] == 1'b0)
			begin
				next[3: 0] = q[3: 0];
				next[4] = ~q[4];
				next[13: 5] = q[13: 5];
			end
		else if (temp[5] == 1'b0)
			begin
				next[4: 0] = q[4: 0];
				next[5] = ~q[5];
				next[13: 6] = q[13: 6];
			end
		else if (temp[6] == 1'b0)
			begin
				next[5: 0] = q[5: 0];
				next[6] = ~q[6];
				next[13: 7] = q[13: 7];
			end
		else if (temp[7] == 1'b0)
			begin
				next[6: 0] = q[6: 0];
				next[7] = ~q[7];
				next[13: 8] = q[13: 8];
			end
		else if (temp[8] == 1'b0)
			begin
				next[7: 0] = q[7: 0];
				next[8] = ~q[8];
				next[13: 9] = q[13: 9];
			end
		else if (temp[9] == 1'b0)
			begin
				next[8: 0] = q[8: 0];
				next[9] = ~q[9];
				next[13: 10] = q[13: 10];
			end
		else if (temp[10] == 1'b0)
			begin
				next[9: 0] = q[9: 0];
				next[10] = ~q[10];
				next[13: 11] = q[13: 11];
			end
		else if (temp[11] == 1'b0)
			begin
				next[10: 0] = q[10: 0];
				next[11] = ~q[11];
				next[13: 12] = q[13: 12];
			end
		else if (temp[12] == 1'b0)
			begin
				next[11: 0] = q[11: 0];
				next[12] = ~q[12];
				next[13] = q[13];
			end
		else
			begin
				next[12: 0] = q[12: 0];
				next[13] = ~q[13];
			end
	end

endmodule
